<template>
  <div class="cesium-draw">
    <div id="cesium-drawer"><div id="slider"></div></div>
    <canvas id="canvas-a" class="canvas" width="700" height="100"></canvas>
    <canvas id="canvas-b" class="canvas" width="700" height="100"></canvas>
    <div class="mouse-postion">{{ nowPostion.wgs84 }}</div>
    <div id="menu">
      <!--<p><button :class="{active:!!synchronizer}" @click="handleClick('synchronizer')">同步</button></p>
      <p><button :class="{active:isRepeating}" @click="handleClick('isRepeating')">重复</button></p>
      <p><button :class="{active:videoOverlay}" @click="handleClick('videoOverlay')">播放器</button></p> -->
    </div>
  </div>
</template>

<script>
import { DownOutlined, EditOutlined } from '@ant-design/icons-vue';
import { defineComponent, ref, watch } from 'vue';
import * as Cesium from '@cesium/Source/Cesium.js';
import MousePosition from '@functional/utils/mousePosition';

export default defineComponent({
  name: 'layerSpliter', // 卷帘
  components: { DownOutlined, EditOutlined },
  setup() {
    return {
      viewer: null,
      synchronizer: null,
      isRepeating: true,
      videoOverlay: true
    };
  },
  data() {
    return {
      nowPostion: {}
    };
  },
  computed: {},
  watch: {},
  mounted() {
    this.initViewer();
  },
  beforeUnmount() {
    if (this.viewer) {
      this.viewer.destroy();
      this.viewer = null;
    }
  },
  methods: {
    onViewerInited(viewer) {
      let vm = this;
      new MousePosition(viewer, pos => {
        vm.nowPostion = pos;
      });
    },

    initViewer() {
      let vm = this;
      Cesium.Ion.defaultAccessToken = $accessToken.default;
      // let osm = new Cesium.OpenStreetMapImageryProvider({
      //   url: 'https://a.tile.openstreetmap.org/'
      // });
      let osm = new Cesium.ArcGisMapServerImageryProvider({
        url: '//services.arcgisonline.com/ArcGIS/rest/services/World_Street_Map/MapServer'
        // url: '//services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer'
      });
      var viewer = new Cesium.Viewer('cesium-drawer', {
        imageryProvider: osm,
        contextOptions: {
          webgl: {
            alpha: true
          }
        },
        animation: false,
        // baseLayerPicker: false,
        fullscreenButton: false,
        vrButton: false,
        homeButton: false,
        timeline: false,
        selectionIndicator: false,
        infoBox: false,
        // 注释时相当于使用默认地形，解开注释相当于使用全球地形
        // terrainProvider,
        shouldAnimate: true
      });

      //开启深度检测
      viewer.scene.globe.depthTestAgainstTerrain = true;

      this.viewer = viewer;
      this.onViewerInited(viewer);

      this.initEffects();

      // this.viewer.flyTo(this.viewer.entities);
    },
    initEffects() {
      let vm = this;
      const viewer = this.viewer;

      var layers = viewer.imageryLayers;
      var earthAtNight = layers.addImageryProvider(
        new Cesium.ArcGisMapServerImageryProvider({
          url: '//services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer'
        })
      );
      earthAtNight.splitDirection = Cesium.ImagerySplitDirection.LEFT; // Only show to the left of the slider.

      // Sync the position of the slider with the split position
      var slider = document.getElementById('slider');
      viewer.scene.imagerySplitPosition = slider.offsetLeft / slider.parentElement.offsetWidth;

      var handler = new Cesium.ScreenSpaceEventHandler(slider);

      var moveActive = false;

      function move(movement) {
        if (!moveActive) {
          return;
        }

        var relativeOffset = movement.endPosition.x;
        var splitPosition = (slider.offsetLeft + relativeOffset) / slider.parentElement.offsetWidth;
        slider.style.left = 100.0 * splitPosition + '%';
        viewer.scene.imagerySplitPosition = splitPosition;
      }

      handler.setInputAction(function() {
        moveActive = true;
      }, Cesium.ScreenSpaceEventType.LEFT_DOWN);
      handler.setInputAction(function() {
        moveActive = true;
      }, Cesium.ScreenSpaceEventType.PINCH_START);

      handler.setInputAction(move, Cesium.ScreenSpaceEventType.MOUSE_MOVE);
      handler.setInputAction(move, Cesium.ScreenSpaceEventType.PINCH_MOVE);

      handler.setInputAction(function() {
        moveActive = false;
      }, Cesium.ScreenSpaceEventType.LEFT_UP);
      handler.setInputAction(function() {
        moveActive = false;
      }, Cesium.ScreenSpaceEventType.PINCH_END);

      // const offset = new Cesium.HeadingPitchRange(0.5, -0.5, 50.0);
      // viewer.flyTo(pointCloud, { offset });
    },
    handleClick(value) {
      const viewer = this.viewer;
      switch (value) {
        case 'isRepeating':
          break;
      }
    }
  }
});
</script>

<style scoped lang="less">
.cesium-draw {
  position: relative;
  width: 100%;
  height: 100%;
  overflow: hidden;
  color: white;
  font-size: 14px;

  #cesium-drawer {
    width: 100%;
    height: 100%;
    overflow: hidden;
    :deep(.cesium-viewer-bottom) {
      display: none !important;
    }
  }

  .mouse-postion {
    position: absolute;
    bottom: 10px;
    right: 10px;
    font-size: 14px;
    padding: 8px 12px;
    line-height: 16px;
    color: white;
    z-index: 9999;
    background-color: rgba(40, 48, 60, 1);
    border-radius: 3px;
    opacity: 0.8;
  }
}

.chart-container {
  position: absolute;
  bottom: 7px;
  left: 7px;
  background-color: rgba(0, 0, 0, 0.5);

  #profileChart {
    width: 600px;
    height: 300px;
  }
}
#canvas-a {
  top: 10px;
  display: none;
}

#canvas-b {
  top: 120px;
  display: none;
}
#trailer {
  position: absolute;
  bottom: 75px;
  right: 0;
  width: 320px;
  height: 180px;
}
#menu {
  position: absolute;
  top: 80px;
  left: 10px;
  z-index: 999;

  button {
    background-color: rgba(0, 0, 0, 0.5);
    border-color: rgba(0, 0, 0, 0);
    cursor: pointer;

    transition: all 500ms;

    &.active {
      background-color: rgba(100, 0, 100, 0.75);
    }

    &:hover {
      opacity: 0.75;
    }
  }
}
#slider {
  position: absolute;
  left: 50%;
  top: 0px;
  background-color: #ff0000;
  width: 5px;
  height: 100%;
  z-index: 9999;
}

#slider:hover {
  cursor: ew-resize;
}
</style>
